package main.java.scenes;

import org.xith3d.base.Xith3DEnvironment;
import org.xith3d.loop.UpdatingThread.TimingMode;
import org.xith3d.render.Canvas3D;
import org.xith3d.scenegraph.BranchGroup;
import org.xith3d.scenegraph.Transform3D;
import org.xith3d.scenegraph.TransformGroup;
import org.xith3d.scenegraph.primitives.Cube;
import org.xith3d.utility.interpolate.AngleInterpolater;

import main.java.MainLoop;
import main.java.ResourceManager;

/**
 * Graphic Experiments/Test
 * 
 * Not Working for some people... 
 * 
 * @author Sven Berger
 * 
 * 
 * 
 */

public class TestScene implements SceneInterface {
	private Transform3D t3d;
	private TransformGroup tg;
	private AngleInterpolater angleX;
	private AngleInterpolater angleY;
	private ResourceManager ress;
	private Canvas3D canvas3D;
	private Xith3DEnvironment xEnv;
	private MainLoop mainLoop;

	private BranchGroup createScene() {
		final Cube cube = new Cube(3.0f, "stone.jpg");
		cube.setName( "cube" );

		this.t3d = new Transform3D();
		this.tg = new TransformGroup(t3d);
		this.tg.addChild(cube);

		return (new BranchGroup(this.tg));
	}

	@Override
	public void onRegister() {
		//create constant anglular speed interpolater with 1.0 pi per second
		this.angleX = new AngleInterpolater(1.0f);
		this.angleY = new AngleInterpolater(1.0f);

		//start interpolation at time...
		this.angleX.startIncreasing(this.mainLoop.getGameMicroTime());
		this.angleY.startIncreasing(this.mainLoop.getGameMicroTime());
	}
	
    public void prepareNextFrame( long gameTime, long frameTime, TimingMode tm )
    {
        //super.prepareNextFrame( gameTime, frameTime, tm );
        
        final long micros = this.mainLoop.getTimingMode().getMicroSeconds( gameTime );
        
        //rotate about x and y axis
        this.t3d.rotXYZ( this.angleX.getValue( gameTime ), this.angleY.getValue( gameTime ), 0.0f );
        this.tg.setTransform( this.t3d );
    }

	@Override
	public BranchGroup registerScene(Xith3DEnvironment env, Canvas3D canvas,
			final ResourceManager resourceManager, MainLoop loop) {
		this.ress=resourceManager;
		this.canvas3D=canvas;
		this.xEnv=env;
		this.mainLoop=loop;
		canvas.setBackgroundColor(0.0f, 0.0f, 0.2f);
		BranchGroup br=createScene();
		env.addPerspectiveBranch(br);
		return(br);
		
	}

	@Override
	public void unregisterScene() {
		this.xEnv.removeAllBranchGraphs();
		
	}
}
